package co.fitstart.weapp.api.web.controller.profile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.OrderType;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.Agent;
import co.fitstart.entity.user.agent.AgentDiscount;
import co.fitstart.entity.user.agent.AgentRecord;
import co.fitstart.weapp.api.service.OrderService;
import co.fitstart.weapp.api.service.UserAgentService;
import co.fitstart.weapp.api.service.UserService;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping(value = "/profile-online-agent")
public class ProfileOnlineAgentController {
	
	@Autowired
	private UserAgentService userAgentService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 
	 * @param unionid
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
	public HttpResult index(@RequestParam("unionid") String unionid) {
		
		HttpResult httpResult = new HttpResult();
		
		try {
            
		    User user = userService.load(unionid);
	        Agent agent = userAgentService.load(user);
	        
	        List<AgentDiscount> agentDiscounts = userAgentService.listDiscount(agent);
	        
	        httpResult.success().message("加载数据成功")
	                            .data("agent", agent)
	                            .data("agentDiscounts", agentDiscounts);
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
		
		return httpResult;
	}
	
	/**
	 * 
	 * @param unionid
	 * @return
	 */
	@RequestMapping(value = "/account", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
	public HttpResult editAccount(@RequestParam("unionid") String unionid) {
		
	    HttpResult httpResult = new HttpResult();
		
		try {
		    
		    User user = userService.load(unionid);
			Agent agent = userAgentService.load(user);
			
			httpResult.success().data(agent.describe());
			
		} catch (Exception e) {
			
		    httpResult.fail("500").message("获取账户信息失败");
		}
		
		return httpResult;
	}
	
	/**
	 * 
	 */
	@RequestMapping(value = "/account", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
	public HttpResult onEditAccount(@RequestParam("unionid") String unionid,
                                    @RequestParam("account") String account,
                                    @RequestParam("realName") String realName) {
		
	    HttpResult httpResult = new HttpResult();
		
		try {
			
		    User user = userService.load(unionid);
			Agent agent = userAgentService.load(user);
			userAgentService.editAccount(agent, account, realName);
			
			httpResult.success().message("编辑账户信息成功");
			
		} catch (Exception e) {
			
		    httpResult.fail("500").message("编辑账户信息失败");
		}
		
		return httpResult;
	}
	
	/**
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/student", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
	public HttpResult agentStudent(@RequestParam("unionid") String unionid) {
	    
	    HttpResult httpResult = new HttpResult();
	    
	    try {
            
	        User user = userService.load(unionid);
	        Agent agent = userAgentService.load(user);
	        
	        List<Order> orders = new ArrayList<Order>();
	        if(agent != null) {
	            orders = orderService.listAgentOrder(OrderType.ONLINE, agent);
	        }
	        httpResult.success().message("获取数据成功").data("orders", orders);
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
	    
	    return httpResult;
	}
	
	/**
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/agent-record", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult agentRecord(@RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            Agent agent = userAgentService.load(user);
            
            List<AgentRecord> agentRecords = userAgentService.listAgentRecord(agent);
            
            httpResult.success().message("加载数据成功")
                                .data("agent", agent)
                                .data("agentRecords", agentRecords);
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "/agent-record/withdraw", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult withdraw(@RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            Agent agent = userAgentService.load(user);
            
            httpResult.success().data(agent.withdrawDescribe());
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("获取提现信息失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param unionid
     * @param offer
     * @return
     */
    @RequestMapping(value = "/agent-record/withdraw", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult onWithdraw(@RequestParam("unionid") String unionid,
                                           @RequestParam("offer") BigDecimal offer) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            Agent agent = userAgentService.load(user);
            
            userAgentService.withdraw(agent, offer);
            
            httpResult.success().message("提现申请提交成功");
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("提现申请提交失败");
        }
        
        return httpResult;
    }
	
}
